/**
 *
 */
package cn.rocksolid.sport.common.utils;

import cn.rocksolid.sport.common.C;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @author axl
 *
 */
public class QwUtils {

  private QwUtils() {
  }


  public static <T> QueryWrapper<T> fuzzy(final Map<String, Object> cond, final String... columns) {
    return fuzzy(new JSONObject(cond).getString(C.FUZZY.p), columns);
  }

  public static <T> QueryWrapper<T> fuzzy(final JSONObject cond, final String... columns) {
    return fuzzy(cond.getString(C.FUZZY.p), columns);
  }

  public static <T> QueryWrapper<T> fuzzy(final String fuzzy, final String... columns) {
    QueryWrapper<T> qw = new QueryWrapper<>();
    if (StringUtils.isNotBlank(fuzzy)) {
      for (String column : ArrayUtils.nullToEmpty(columns)) {
        qw.like(column, fuzzy).or();
      }
    }
    return qw;
  }

  public static <T> QueryWrapper<T> fuzzy(final QueryWrapper<T> qw, final Map<String, Object> cond,
          final String... columns) {
    return fuzzy(qw, new JSONObject(cond).getString(C.FUZZY.p), columns);
  }

  public static <T> QueryWrapper<T> fuzzy(final QueryWrapper<T> qw, final JSONObject cond, final String... columns) {
    return fuzzy(qw, cond.getString(C.FUZZY.p), columns);
  }

  public static <T> QueryWrapper<T> fuzzy(final QueryWrapper<T> qw, final String fuzzy, final String... columns) {
    qw.and(StringUtils.isNotBlank(fuzzy), q -> {
      for (String column : ArrayUtils.nullToEmpty(columns)) {
        q.like(column, fuzzy).or();
      }
    });
    return qw;
  }
}
